<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
  public function login(){
    $m = M("cate");
    $cateList1 = $m->where('fid = 1')->select();
    $cateList2 = $m->where('fid = 2')->select();
    $this->assign('cateList1',$cateList1);
    $this->assign('cateList2',$cateList2);
    $this->display();
  }

  public function checkUser(){
    $username = $_POST['username'];
    $password = md5($_POST['password']);
    $m = M("user");
    $result = $m->where("username='%s' AND password='%s' AND status=0",$username,$password)->find();
    if($result>0){
      $_SESSION['nickname'] = $result['nickname'];
      $_SESSION['username'] = $result['username'];
      $this->success('登录成功,三秒后跳转主页。', U('Home/Index/index'),3);
    }else{
      $this->error("用户名或密码错误。");
    }
  }

  public  function logout(){
    session(null);
    $this->success('欢迎再来!',U('Home/Index/index'),3);
  }

  public function register(){
    $m = M("cate");
    $cateList1 = $m->where('fid = 1')->select();
    $cateList2 = $m->where('fid = 2')->select();
    $this->assign('cateList1',$cateList1);
    $this->assign('cateList2',$cateList2);
    $this->display();
  }

  public function doRegister(){
    $m = M('user');
    $m->create();
    $m->password = md5($_POST['password']);
    $m->status = 0;
    $m->regtime = time(); 
    $result = $m->add(); 
    if($result>0){
      $_SESSION['nickname'] = $_POST['nickname'];
      $_SESSION['username'] = $_POST['username'];
      $this->success('注册成功,三秒后跳转主页。', U('Home/Index/index'),3);
    }else{
      $this->error("注册失败");
    }
  }
}
